extends IBasicComponent


class_name InputTrigger
"""
输入触发
"""


## 是否处理 _input
@export var do_input: bool = true
## 是否处理 _unhanled_input
@export var do_unhanled_input: bool = false
## 是否处理 _unhanled_key_input
@export var do_unhanled_key_input: bool = false

## 需要适配的GUI输入事件
@export var do_gui: Control = null

@export_category("Action")
## 输入检测
@export var inputs: Array[IInputAction] = []
@export_category("")


func __check_exports() -> Dictionary:
    return IComponent.merge({
        "inputs": IComponent.find_children.bind(self, func(x): return x is IInputAction, inputs),
        "do_gui": [IComponent.find_parent.bind(self, func(x): return x is Control, do_gui)],
    }, super.__check_exports())

func __init_hooks() -> Dictionary:
    var dict: Dictionary = {do_gui.gui_input: on_input_event} if do_gui else {}
    return IComponent.merge(dict, super.__init_hooks())

func on_input_event(e: InputEvent) -> void:
    if IComponent.is_available(self):
        for input in inputs:
            input.yield_input(e)

func _input(event):
    if do_input:
        on_input_event(event)

func _unhandled_input(event):
    if do_unhanled_input:
        on_input_event(event)

func _unhandled_key_input(event):
    if do_unhanled_key_input:
        on_input_event(event)
